﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using VirtuOS.Components;
using VirtuOS.Base;

namespace VirtuOS.Components.Srvcs
{
    class Session
    {
        public static void Load()
        {
            Login();
        }

        private static void Login()
        {
            if (Environment.CurrentDirectory != VirtuOS.vdir_full) { Environment.CurrentDirectory = VirtuOS.vdir_full; }
            Console.Title = "VirtuOS";
            bool li = false;
            string machine = TextHandler.UpperFirst(Environment.MachineName.ToLower());
            Console.WriteLine("\n" + Environment.OSVersion + " (" + machine + ")");
            Console.Write("\n\n" + machine + " login: ");
            string user = Console.ReadLine();

            StreamReader sr = File.OpenText("etc/users/passwds");
            while (!sr.EndOfStream)
            {
                string cl = sr.ReadLine();
                string[] spl = cl.Split(':');
                if (spl[0].Equals(user))
                {
                    string uname = spl[0];
                    string strbyte = spl[1];
                    string pwdhash = spl[2];

                    string[] bytes = strbyte.Split('-');
                    byte[] salt = new byte[bytes.Length];
                    for (int i = 0; i < salt.Length; i++)
                    {
                        salt[i] = Convert.ToByte(bytes[i]);
                    }
                    string inputhash = HashHandler.ComputeHash(InputHandler.Masked("Password: "), "SHA512", salt);
                    if (inputhash.Equals(pwdhash))
                    {
                        Console.WriteLine("\nWelcome back, {0}.\n", spl[0]);
                        Properties.Settings.Default.HOMEDIR = "/home/" + spl[0];
                        Properties.Settings.Default.USER = spl[0];
                        li = true;
                        UpdateEnVars(uname);
                        sr.Close();
                        Environment.CurrentDirectory = VirtuOS.vdir_full + "/home/" + user;
                        Console.Title = "VirtuOS · " + Properties.Settings.Default.USER + " · " + FileHandler.CurrentDirectory();
                        Terminal term = new Terminal();
                        term.Vconsole();
                    }
                    else
                    {
                        Console.WriteLine("Incorrect password.");
                        sr.Close();
                        Login();
                    }
                }
            }
            sr.Close();
            if (li == false)
            {
                Login();
            }
        }

        public static void Logout()
        {
            Console.Clear();
            Login();
        }

        private static void UpdateEnVars(string user)
        {
            StreamReader sr = new StreamReader("home/" + user + "/variables.gr");
            while (!sr.EndOfStream)
            {
                string cl = sr.ReadLine();
                if (cl.Contains("PS1"))
                {
                    string[] ps1a = cl.Split('=');
                    string ps1 = ps1a[1];
                    ps1 = ps1.Replace("\"", "");
                    Properties.Settings.Default.PS1 = ps1;
                }
            }
            Properties.Settings.Default.USER = user;
            Properties.Settings.Default.HOMEDIR = "/home/" + user;
        }
    }
}
